package com.gonfre.quientelo;

import android.os.StrictMode;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;

import util.AsyncRequest;
import util.ChipsMultiAutoCompleteTextview;
import util.DataBaseManager;
import util.MyDBObject;
import util.MyDataBase;
import util.Response;


public class MainActivity extends MyActivity {

    ChipsMultiAutoCompleteTextview mu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mu = (ChipsMultiAutoCompleteTextview) findViewById(R.id.multiAutoCompleteTextView1);

        //String[] item = getResources().getStringArray(R.array.country);
        DataBaseManager dbm = new DataBaseManager(this);
        String table = this.getString(R.string.table_ubicacion);
        String sql_table = this.getString(R.string.sql_ubicacion);
        String query_item = this.getString(R.string.query_item_ubicacion);
        MyDataBase dbLocations = new util.MyDataBase(this, dbm, table, sql_table);
        String[] item = dbLocations.readStrItem(query_item, "", "ubicacion", "id");

        Log.i("", "Country Count : " + item.length);
        mu.setAdapter(new ArrayAdapter(this,
                android.R.layout.simple_dropdown_item_1line, item));
        mu.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }

    public void buttonClicked(View v) {
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText(mu.getText().toString());
    }

    public void buttonOnClick(View w) {

        AsyncRequest req = new AsyncRequest(this);
        Response res = new Response();

        TextView t0 = (TextView)findViewById(R.id.editTextIdPeticion);
        String tp = t0.getText().toString();

        switch (tp){
            case "1":
                res = req.xLogin("carlos@gmail.com", "carlos");
                break;
            case "2":
                res = req.xRegisterSeller("carlos","0000000","carlos@gmail.com","carlos",1,"1,2,3", "123");
                break;
            case "3":
                res = req.xRegistrerPurchaser("juan","0000000","juan@gmail.com","juan",2);
                break;
            case "5":
                res = req.xGetLocationList("Sab");
                break;
            case "6":
                res = req.xGetCategoryList("Co");
                break;
            case "7":
                res = req.xGetCountryList();
                break;
            case "16":
                res = req.xGetLocationListByCountry(75, getVersionDB(getString(R.string.table_ubicacion)));
                break;
            case "17":
                res = req.xGetFullCategoryList(getVersionDB(getString(R.string.table_categoria)));
                break;
            case "99":
                /*
                TextView t1 = (TextView)findViewById(R.id.editTextBodyResponse);
                int tv = Integer.parseInt(t1.getText().toString());

                //Abrimos la base de datos 'DataBase' en modo escritura
                DataBaseManager dbm = new DataBaseManager();
                SQLiteDatabase db = dbm.getDataBase(this, R.string.table_ubicacion, R.string.sql_ubicacion, tv);

                if(db != null) {
                    Log.i("Trace MainActivity 'onCreate'", "Trace Database OK");
                }else{
                    Log.i("Trace MainActivity 'onCreate'", "Trace Database KO");
                }
                db.close();
                */
                DataBaseManager dbm = new DataBaseManager(this);
                dbm.syncLocation();

                break;
            default:
                Log.i("Trace 'buttonOnClick'", "Peticion '" + tp + "' NO configurada");
        }



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFinishRequest(int type, Response res) {
        TextView t1 = (TextView)findViewById(R.id.editTextBodyResponse);
        t1.setText(res.getHeaderResponse().toString());
        TextView t2 = (TextView)findViewById(R.id.editTextBodyMessage);
        t2.setText(res.getHeaderMessage());
        /*
        * La respuesta Obtenida se lee con los sgtes metodos:
        * getResultStr() -> Retorna la respuesta en String obtenida de la peticion post
        * res.getBodyStr() -> Retorna el body recibido en string
        * res.getHeaderResponse() -> Retorna el valor del atributo "response" del header
        * res.getHeaderMessage() -> Retorna el valor del atributo "message" del header
        * res.getBodyValueStr("x") -> Retorna valor del atributo String "x" que se encuentra en el body
        * res.getBodyValueInt("x") -> Retorna valor del atributo Integer "x" que se encuentra en el body
        * res.getBodyValueArrayStr("list", i, "x") -> Retorna valor del atributo String "x" que se encuentra en la lista "list" en el registro i
        * res.getBodyValueArrayInt("list", i, "x") -> Retorna valor del atributo Integer "x" que se encuentra en la lista "list" en el registro i
        *
        * */
    }
}
